<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%@ include file="../taglibs.jsp"%>
<%@taglib prefix="staticResource" tagdir="/WEB-INF/tags/header"%>
<staticResource:staticResource></staticResource:staticResource>
<title><fmt:message key="application.title" /></title>
<style type="text/css">
b {
	color: #666;
}
</style>
</head>
<body class="easyui-layout">
	<div data-options="region:'north',border:false" style="height: 32px; background: #FFF; padding: 0px">
		<div class="navBox">
			<table height="100%">
				<tr>
					<td>
						<img src="${ctx}/static/images/home/icon0.png" />
					</td>
					<td>${set.sysName}&gt;&gt;系统管理&gt;&gt; 角色管理</td>
				</tr>
			</table>
		</div>
	</div>

	<div data-options="region:'center',title:''" style="overflow-x: hidden; padding: 5px;">
		<div id="toolbar" style="padding: 3px">
			<div>
				<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="add()">添加</a>
				<a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="edit()">修改</a>
				<a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="deleteAll()">删除</a>
				<a href="#" class="easyui-linkbutton" iconCls="icon-link" plain="true" onclick="bindMenu();">绑定菜单</a>
				<a href="#" class="easyui-linkbutton" iconCls="icon-link" plain="true" onclick="projectMenu.bind();">绑定工况菜单</a>
			</div>
		</div>
		<div style="float: left; width: 100%; height: auto; padding: 2px">
			<div style="float: left; width: 100%; margin-top: 5px;">
				<table id="dg">
				</table>
			</div>
		</div>
	</div>

	<!--form  -->
	<div id="dlg" class="easyui-dialog" closed="true" data-options="modal:true" style="width: 350px; height: auto;">
		<form id="fm" method="post">
			<input type="hidden" name="roleId">
			<table id="table3" width="99%" height="210px">
				<tr>
					<td height="40px" colspan="4" align="center" valign="middle">
						<b>角色信息</b>
					</td>
				<tr>
					<td align="center">角色名称</td>
					<td>
						<input class="easyui-validatebox" type="text" name="roleName">
					</td>
				</tr>
				<tr>
					<td align="center">角色描述</td>
					<td colspan="3">
						<textarea rows="3" cols="3" style="width: 98%; height: 100%" name="description"></textarea>
					</td>
				</tr>
				<tr align="center">
					<td height="30" colspan="4" align="center">
						<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="save()">确定</a>
						&nbsp;&nbsp;
						<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">取消</a>
					</td>
				</tr>
			</table>
		</form>
	</div>

	<!-- 菜单 树-->
	<div id="showMenuTreeWindow" class="easyui-dialog" style="width: 300px; height: 500px; padding: 10px 20px" closed="true" buttons="#bindMenu-buttons">
		<form id="menuTreeForm" method="post">
			<input type="hidden" name="roleId" id="hidden_roleId">
			<input type="hidden" name="menuIds" id="hidden_menuIds">
			<ul id="menuTree"></ul>
			<div id="bindMenu-buttons">
				<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveBindMenu()">确定</a>
				<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#showMenuTreeWindow').dialog('close')">取消</a>
			</div>
		</form>
	</div>
	
	<!-- 工况 菜单 树-->
	<div id="projectMenu-bind" class="easyui-dialog" style="width: 300px; height: 500px; padding: 10px 20px" closed="true" buttons="#projectMenu-buttons">
		<form id="projectMenu-bind_form" method="post">
			<input type="hidden" name="roleId" id="projectMenu-bind_roleId">
			<input type="hidden" name="menuIds" id="projectMenu-bind_menuIds">
			<ul id="projectMenu-tree"></ul>
			<div id="projectMenu-buttons">
				<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="projectMenu.save()">确定</a>
				<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#projectMenu-bind').dialog('close')">取消</a>
			</div>
		</form>
	</div>
</body>

<!-- 实时数据加载 -->
<script type="text/javascript">
	$(function() {
		// 获取浏览器窗口 
		var windowScreen = document.documentElement;
		var h = windowScreen.clientHeight;
		$('#dg').datagrid({
			url : '${ctx}/system/role/list',
			height : (h - 50),
			nowrap : true,
			autoRowHeight : true,
			striped : false,
			collapsible : false,
			fitColumns : true,
			remoteSort : false,
			singleSelect:true,
			pagination : true,//显示分页 
			rownumbers:true,
			idField : 'roleId',
			toolbar : $("#toolbar"),
			columns : [ [ {
				field : 'roleId',
				title : '用户ID',
				hidden : true
			}, {
				field : 'roleName',
				title : '角色名称',
				width : 10
			}, {
				field : 'description',
				title : '角色描述',
				width : 20,
				align : 'left'}
			] ]
		});
	});
	var url;
	//add
	function add() {
		$('#dlg').dialog('open').dialog('setTitle', '添加角色');
		$('#fm').form('clear');
		url = '${ctx}/system/role/save';
	}

	//edit
	function edit() {
		var rows = $('#dg').datagrid('getSelections');
		if (rows.length != 1) {
			$.messager.alert('提示', '请选择一条记录', 'info');
		} else {
			$('#dlg').dialog('open').dialog('setTitle', '修改角色');
			$('#fm').form('load', rows[0]);
			url = '${ctx}/system/role/update';
		}
	}
	//save
	function save() {
		$('#fm').form('submit', {
			url : url,
			onSubmit : function() {
				return $(this).form('validate');
			},
			success : function(response) {
				response = eval('(' + response + ')');
				$.messager.show({
					title : response.title,
					msg : response.msg,
					timeout : response.timeout,
					showType : response.showType
				});
				$('#dlg').dialog('close');
				$('#dg').datagrid('reload');
			}
		});
	}
	//deleteAll
	function deleteAll() {
		var rows = $('#dg').datagrid('getSelections');
		if (null == rows || rows.length < 1) {
			$.messager.alert('提示', '请选择一条记录', 'info');
		} else {
			$.post('${ctx}/system/role/checkBind?roleId=' + rows[0].roleId, function(
					response) {
				if(response){			
					$.messager.confirm('提示', '确认删除选中的记录吗?', function(r) {
						//获取datagrid选中行
						var ids = [];
						for ( var i = 0; i < rows.length; i++) {
							ids.push(rows[i].roleId);
						}
						if (r) {
							$.post('${ctx}/system/role/deleteAll?ids=' + ids, function(
									response) {
								$.messager.show({
									title : response.title,
									msg : response.msg,
									timeout : response.timeout,
									showType : response.showType
								});
								$('#dlg').dialog('close');
								$('#dg').datagrid('reload');
							}, 'json');
						}
					});					
				}else{
					$.messager.alert('提示', '请先解除与用户的绑定关系', 'info');
				}
				
			}, 'json');

		}
	}
	function bindMenu(){
		var rows = $('#dg').datagrid('getSelections');
		if(rows.length!=1){
			$.messager.alert('提示','请选择一条记录','info');
			return;
		}{
			$('#menuTree').tree({
				url:'${ctx}/system/menu/tree',
				animate:true,
				checkbox:true,
				onLoadSuccess:function(){  
	                 //绑定项目  
	                  $.ajax({  
	                     url:'${ctx}/system/menu/getByRole?roleId='+rows[0].roleId,  
	                      cache:false,  
	                      dataType:'json',  
	                      success:function(data){  
	                    	  //默认选中已经关联的菜单
	                     	  if(data.rows.length>0){
	                     		 var node;
	 	                         for(var i=0;i<data.rows.length;i++){  
	 	                        	 node = $('#menuTree').tree('find',data.rows[i].menuId);  
	 	                        	 if($('#menuTree').tree('isLeaf',node.target)){	 	                        		 
	 	                             	 $('#menuTree').tree('check',node.target);  
	 	                        	 }
 	                          }  
	                     }   
	                     }  
	                 });  
	             }  
			});	
	    	$('#hidden_roleId').val(rows[0].roleId);
			$('#showMenuTreeWindow').dialog('open').dialog('setTitle','关联菜单');
		}
	}
	function saveBindMenu(){
		var node = $('#menuTree').tree('getChecked', ['checked','indeterminate']);
		var menuIds=[];
	 	for(var i=0;i<node.length;i++){
		 	menuIds.push(node[i].id);
  	 	}
	    $('#hidden_menuIds').val(menuIds);
	 	$('#menuTreeForm').form('submit', {
			url :'${ctx}/system/role/saveBindMenu',
			onSubmit : function() {
				return $(this).form('validate');
			},
			success : function(response) {
				//绑定成功后需要退出重新登录后生效
				$.messager.confirm('提示', '绑定成功后需要重新登录,确认重新登录吗?', function(r) {
					if (r) {
						 var url = '${ctx}/logout';
						 window.parent.location = url;
					}
				});
			}
		});
	}
	
	var projectMenu = {
		bind:function(){
			var rows = $('#dg').datagrid('getSelections');
			if(rows.length!=1){
				$.messager.alert('提示','请选择一条记录','info');
				return;
			}{
				$('#projectMenu-tree').tree({
					url:'${ctx}/system/menu/allProjectTree',
					animate:true,
					checkbox:true,
					onLoadSuccess:function(){  
		                 //绑定项目  
		                  $.ajax({  
		                     url:'${ctx}/tSysRole2menuProject/getTSysRole2menuProjectList?page=1&rows=10000&roleId='+rows[0].roleId,  
		                      cache:false,  
		                      dataType:'json',  
		                      success:function(data){  
		                    	  //默认选中已经关联的菜单
		                     	  if(data.rows.length>0){
		                     		 var node;
		 	                         for(var i=0;i<data.rows.length;i++){  
		 	                        	 node = $('#projectMenu-tree').tree('find',data.rows[i].menuId);  
		 	                        	 if($('#projectMenu-tree').tree('isLeaf',node.target)){	 	                        		 
		 	                             	 $('#projectMenu-tree').tree('check',node.target);  
		 	                        	 }
	 	                          }  
		                     }   
		                     }  
		                 });  
		             }  
				});	
		    	$('#projectMenu-bind_roleId').val(rows[0].roleId);
				$('#projectMenu-bind').dialog('open').dialog('setTitle','关联菜单');
			}
		},
		save:function(){
			var node = $('#projectMenu-tree').tree('getChecked', ['checked','indeterminate']);
			var menuIds=[];
		 	for(var i=0;i<node.length;i++){
			 	menuIds.push(node[i].id);
	  	 	}
		    $('#projectMenu-bind_menuIds').val(menuIds);
		 	$('#projectMenu-bind_form').form('submit', {
				url :'${ctx}/system/role/saveBindProjectMenu',
				onSubmit : function() {
					return $(this).form('validate');
				},
				success : function(response) {
					//绑定成功后需要退出重新登录后生效
					$.messager.confirm('提示', '绑定成功后需要重新登录,确认重新登录吗?', function(r) {
						if (r) {
							 var url = '${ctx}/logout';
							 window.parent.location = url;
						}
					});
				}
			});
		}
	}
	
	//search
	function doSearch() {
		$('#dg').datagrid('load', {});
	}
</script>
</html>
